package perf_test;

import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipOutputStream;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

/**
 * Servlet implementation class ControllerServlet
 */
@WebServlet("/ControllerServlet")
public class ControllerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	private DataSource ds = null;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ControllerServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public void init(ServletConfig config) throws ServletException{
    	super.init(config);
    	Context ctx = null;
    	try {
    		ctx = new InitialContext();
    		ds = (DataSource) ctx.lookup("jdbc/hr");
    	} catch (NamingException e) {
    		e.printStackTrace();
    	} 
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		OutputStream out = null;
		String encoding = request.getHeader("Accept-Encoding");
		
		if (encoding != null && encoding.indexOf("gzip") != -1) {
			response.setHeader("Content-Encoding", "gzip");
			out = new GZIPOutputStream(response.getOutputStream());
		} else if(encoding != null && encoding.indexOf("compress") != -1){
			response.setHeader("Content-Encoding", "compress");
			out = new ZipOutputStream(response.getOutputStream());
		} else {
			out = response.getOutputStream();
		}
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
